<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\InvoiceTitle as InvoiceTitleModel;

/**
 *发票抬头
 */
class Invoicetitle extends Api
{

    protected $noNeedRight = ['*'];

    //
    public function lists()
    {
        $list = InvoiceTitleModel::where(['user_id'=>$this->auth->id])->select();
        $this->success('',$list);
    }

    public function add()
    {
        $model = new InvoiceTitleModel();

        $data = [
            'user_id'=>$this->auth->id,
            'type'=>input('type',0),
            'taxid'=>input('taxid',''),
            'invoice_title'=>input('invoice_title',''),
            'mobile'=>input('mobile',''),
            'email'=>input('email','')
        ];
        if (empty($data['invoice_title']))$this->error('请输入发票抬头');
        if (empty($data['mobile']))$this->error('请输入手机号');
        if (empty($data['email']))$this->error('请输入邮箱');
        if ($data['type']==1){
            if (empty($data['taxid']))$this->error('请输入税号');
        }
        $res = $model->save($data);
        if ($res){
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

    public function info()
    {
        $id = input('invoice_title_id',0);
        $info = InvoiceTitleModel::get($id);
        $this->success('',$info);
    }

    public function edit()
    {
        $model = new InvoiceTitleModel();
        $id = input('invoice_title_id',0);
        if (empty($id))$this->error('缺少参数id');
        $data = [
            'user_id'=>$this->auth->id,
            'type'=>input('type',0),
            'taxid'=>input('taxid',''),
            'invoice_title'=>input('invoice_title',''),
            'mobile'=>input('mobile',''),
            'email'=>input('email','')
        ];
        if (empty($data['invoice_title']))$this->error('请输入发票抬头');
        if (empty($data['mobile']))$this->error('请输入手机号');
        if (empty($data['email']))$this->error('请输入邮箱');
        if ($data['type']==1){
            if (empty($data['taxid']))$this->error('请输入税号');
        }
        $res = $model->save($data,['invoice_title_id'=>$id]);
        if ($res){
            $this->success('编辑成功');
        } else {
            $this->error('编辑失败');
        }
    }

    public function del()
    {
        $id = input('invoice_title_id',0);
        $info = InvoiceTitleModel::get($id);
        $res = $info->delete();
        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}